package com.learncloud.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.learncloud.common.core.exception.ServiceException;
import com.learncloud.common.core.wrapper.ResponseWrap;
import com.learncloud.common.core.wrapper.ResponseWrapUtil;
import com.learncloud.entity.SysDict;
import com.learncloud.service.ISysDictService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author yj
 */
@RestController
@RequestMapping("/admin/sys-dict")
public class SysDictController {

    @Autowired
    private ISysDictService iSysDictService;

    @PostMapping("add")
    @ApiOperation("新增字典")
    public ResponseWrap add(@RequestBody SysDict sysDict){
        boolean exist = isExist(sysDict.getType(), sysDict.getValue(), null);
        if (exist){
            throw new ServiceException("新增失败,已存在该字典");
        }
        boolean save = iSysDictService.save(sysDict);
        return ResponseWrapUtil.success();
    }

    @PostMapping("update")
    @ApiOperation("修改字典")
    public ResponseWrap update(@RequestBody SysDict sysDict){
        boolean exist = isExist(sysDict.getType(), sysDict.getValue(), sysDict.getId());
        if (exist){
            throw new ServiceException("修改失败,已存在该字典");
        }
        boolean save = iSysDictService.updateById(sysDict);
        return ResponseWrapUtil.success();
    }

    @DeleteMapping("delete")
    @ApiOperation("删除字典")
    public ResponseWrap delete(Integer id){
        boolean remove = iSysDictService.removeById(id);
        return ResponseWrapUtil.success();
    }

    @GetMapping("getTypes")
    @ApiOperation("获取字典类型列表")
    public ResponseWrap getTypes(){
        List types = iSysDictService.getTypes();
        return ResponseWrapUtil.success(types);
    }

    @GetMapping("getList")
    @ApiOperation("获取字典列表")
    public ResponseWrap getList(SysDict sysDict){
        List dicts = iSysDictService.list(new QueryWrapper<>(sysDict).orderByAsc("sort"));
        return ResponseWrapUtil.success(dicts);
    }

    @GetMapping("/getPaginationList")
    @ApiOperation(value = "条件分页查询菜单列表")
    public ResponseWrap<Page<SysDict>> getPaginationList(@ApiParam("页面条数") Integer pageSize, @ApiParam("当前页")Integer pageIndex, SysDict sysDict){
        Page<SysDict> sysDictPage = new Page<>(pageIndex, pageSize);
        List<OrderItem> orders = Arrays.asList(new OrderItem().setColumn("type").setAsc(true),new OrderItem().setColumn("sort").setAsc(true));
        sysDictPage.setOrders(orders);
        Page<SysDict> page = iSysDictService.page(sysDictPage,new QueryWrapper<>(sysDict));
        return ResponseWrapUtil.success(page);
    }

    /**
     * 判断是否已存在字典
     * @param type
     * @param value
     * @param id
     * @return
     */
    private boolean isExist(String type, String value, Integer id){
        SysDict sysDict = new SysDict().setType(type).setValue(value);
        List<SysDict> list = iSysDictService.list(new QueryWrapper<>(sysDict));
        boolean b = false;
        if (list.size() > 0){
            b = true;
            if (id != null && list.size() == 1  && list.get(0).getId().equals(id)){
                b = false;
            }
        }
        return b;
    }
}

